# -*- coding: utf-8 -*-
#打开report.txt文件，将内容按行读取到list列表中，将list列表分为标头列表title和成绩列表scores
f = open('report.txt')
list = f.readlines()
title = list[0]
scores = list[1:]
new_scores = []
stu_number = len(scores)
sub_number = 9
avg_score = ['0','平均']

title = title.strip() + ' 总分 平均分'
title = title.split()

#将成绩列表进行字符串分割
for score in scores:
    new_scores.append(score.split())

#计算每个学生的总分和平均成绩，并添加到每个列表最后
for score in new_scores:
    sum = 0.0
    avg = 0.0
    for j in score[1:]:
        sum += int(j)
    avg = "%.1f" %(sum / sub_number)
    score.append(str(sum))
    score.append(str(avg))

#按照每个学生的总成绩进行排序，并将排名添加至每个列表最前边
new_scores.sort(key=lambda x:x[10],reverse=True)
i = 1
for score in new_scores:
    score.insert(0,str(i))
    i += 1

for i in range(2,13):
    sub_sum = 0.0
    for score in new_scores:
        sub_sum += float(score[i])
    avg = "%.1f" %(sub_sum / stu_number)
    avg_score.append(str(avg))

for i in range(1,len(new_scores)):
    for j in range(2,len(new_scores[i])-2):
        if int(new_scores[i][j]) < 60:
            new_scores[i][j] = '不及格'

new_scores.insert(0,avg_score)
new_scores.insert(0,title)

f = open('new_report.txt','w')
for score in new_scores:
    result = ' '.join(score) + '\n'
    print result
    f.writelines(result)
f.close()